require 'net/http'
require 'uri'
require 'rubygems'
require 'sqlite3'

$url1 = "http://finance.sina.com.cn/realstock/company/%s/nc.shtml"
url2 = "http://hq.sinajs.cn/list=s_%s"

startcode = 'sh601398'

$rel = {}
$codes = {}
$db = SQLite3::Database.new('code.data')

def get(url,level)
  if $rel[url] != nil
    return 0;
  end
  $rel[url]=1
  if level == 0
    return 0
  end
  ur = URI.parse(url)
  html = Net::HTTP.get_response(ur).body
  rel = html.scan(/<a\s+href\s*=\s*['"](.*?)['"]\s*/)
  if rel.length == 0
    return 0;
  end
r = html.scan(/((sh|sz)\d{6})/)
r.each{ |y|
  if $codes[y[0]] != nil
    next
  else
    $codes[y[0]] = 1
    $db.execute("insert into codes (code,name) values ('#{y[0]}','') ")
  end
}
  rel.each do |u|
    x = u[0]
    i = ''
    if /^\s*https/.match(x) || /^\s*javascript/.match(x) || /^\s*mailto/.match(x) || /^\s*#/.match(x)
      next
    elsif /^\s*http/.match(x)
      i = x
    else
      r = %r{(.*?)/[^/]*$}.match(url)
      if %r{^/.*}.match(x)
        i = r[1][0].to_s + x
      else
        if /\d+/.match(r[1][0].to_s)
          next
        end
        i = r[1][0].to_s + '/' + x
      end
    end
    Thread.new do
      get(i,level - 1)
    end
  end
end

def getInfo(c)
  ur = URI.parse(sprintf($url1,c))
  html = Net::HTTP.get_response(ur).body
  name = html.scan(/var\s+stockname\s*=\s*"(.*?)"\s*;/)[0][0]
  lastfive = html.scan(/var\s+lastfive\s*=\s*"(.*?)"\s*;/)[0][0]
  total = html.scan(/var\s+totalcapital\s*=\s*"(.*?)"\s*;/)[0][0]
  cur = html.scan(/var\s+currcapital\s*=\s*"(.*?)"\s*;/)[0][0]
  state = html.scan(/var\s+stock_state\s*=\s*"(.*?)"\s*;/)[0][0]
  price = html.scan(/var\s+hq_str_s_\w{2}\d{6}\s*=\s*"[^,]+?,([^,]*?),"/)[0][0]
  time = Time.now.to_i
  if $db.execute("select * from codes where code = #{c}").empty?
    $db.execute("update codes set name = '#{name}' where code = #{c}")
  end
  $db.execute("insert into info (code, lastfive, total, cur, state, ts, price) values ('#{c}','#{lastfive}','#{total}','#{cur}','#{state}',#{time},'#{price}')")
end

#get(sprintf($url1,startcode),5)
#$codes.each { |k,v|  puts k + '=>' +v.to_s}
#url3 = 'http://finance.sina.com.cn/stock/'
#get('http://www.sina.com.cn/',5)
#get(url3,5)

codes = $db.execute('select * from codes')
if codes.empty?
  get(sprintf($url1,startcode), 5)
  codes = $db.execute('select * from codes')
end
codes.each{|item|
  getInfo(item)
}




